<?php
namespace Api\Event;

/**
 * 常见问题及答案
 * @author liubo <liubo@ilongyuan.com.cn>
 */
class FaqEvent extends BaseEvent
{
    /**
     * 模型对象
     */
    protected $mod;

    /**
     * 成员变量的初始化
     */
    protected function _initialize() {
        parent::_initialize();
        $this->mod = D( 'Common/Faq', 'Service' );
    }

    /**
     * 问题关键字搜索
     * @author liubo <liubo@ilongyuan.com.cn>
     * @param  String $keywords 关键字
     * @return Array
     */
    public function search($keywords, $page = 1, $pager = 15) {
        $condition = ['status' => 1];
        if (!empty($keywords)) {
            $condition['title'] = ['like', $keywords];
        }

        //查询结果
        $data = $this->mod->where($condition)
                          ->field('id,title,times,is_hot')
                          ->limit(($page-1)*$pager, $pager)
                          ->order('sort asc, create_time desc')
                          ->select();
        $totalCount = $this->mod->where($condition)->count();
        return ['list' => $data?:[], 'total' => $totalCount];
    }

    /**
     * 常见的问题
     * @author liubo <liubo@ilongyuan.com.cn>
     * @param  integer $top
     * @return array
     */
    public function getTop($top = 10) {
        $result = $this->mod->where(['status' => 1])->limit($top)->order('times desc')->field('id,title,times,is_hot')->select();
        return $result;
    }

    /**
     * 问题详情
     * @author liubo <liubo@ilongyuan.com.cn>
     * @param  integer $id
     * @return Array
     */
    public function detail($id) {
        $result = $this->mod->where(['id' => $id, 'status' => 1])->field('id,title,content,times,is_hot')->find();

        //查询次数加1
        $this->mod->where(['id' => $id, 'status' => 1])->setInc('times');
        return $result;
    }
}